<?php
/**
 * @file name : contact.php
 * @package   : thokim
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Contact extends Base_Controller
{
	protected $_folderPath = 'application/uploads/captcha';
    /**
     * __construct
     * @access public
     * @param
     * @return
     */
    public function __construct()
    {
        parent::__construct();
		session_start();
    }
    /**
      * _remap
      * @access public
      * @param
      * @return
      */
    public function _remap( $method )
    {
        switch ($method) {

			case 'sendContact':
				$this->sendContact();
				break;

            default:
                $this->contactPage();
        }
    }
	/**
	 * @function	index
	 *
	 *
	 */
	private function contactPage()
	{
		$controlName    = $this->uri->segment(1);
		/*
		$this->load->plugin('captcha');
		$vals = array(
                        'word'         => rand(),
                        'img_path'     => 'application/uploads/captcha/',
                        'img_url'     => base_url().'application/uploads/captcha/',
                        'font_path'     => '../system/fonts/texb.ttf',
                        'img_width'     => '100',
                        'img_height' => '45',
                        'expiration' => '36'
                    );

		$cap = create_captcha($vals);

		$_SESSION['captcha'] = $cap['word'];

		$data = array('imgCaptcha'	=> $cap['image']);
		*/
		$data = array();
		$this->msmarty->assignArrVal($data);
		$main = $this->msmarty->fetch($controlName . '/contact.tpl');
		$this->msmarty->assign('main', $main);

		$this->msmarty->view();
	}
	/**
	 *
	 *
	 *
	 */
	private function sendContact()
	{
		$controlName    = $this->uri->segment(1);

		$confirmCode = $this->input->post('confirm_code');
		if ($this->checkValidate($confirmCode)) {
			echo 'bang session :' . $confirmCode . ' - ' . $_SESSION['captcha'];
		} else {
			echo 'Khong bang session :' . $confirmCode . ' - ' . $_SESSION['captcha'];
		}
		$this->session->sess_destroy();
		$main = $this->msmarty->fetch($controlName . '/contact.tpl');
		$this->msmarty->assign('main', $main);
		$this->msmarty->view();
	}
	/**
	 *
	 *
	 */
	private function checkValidate($key)
	{
		//return $this->session->userdata('captcha') == $key;
		return $_SESSION['captcha'] == $key;
	}
	/**
	 * deleteOldImg
	 *
	 */
	private function deleteOldImg($dir)
	{
		if ( ! is_dir($dir)) {
			return false;
		}
		$arrDir = scandir($dir);
		foreach($arrDir as $file) {
			if ($file == '.' || $file == '..' || $file == '.svn') {
				continue;
			}
			if (file_exists($this->_folderPath . '/' . $file)) {
				unlink($this->_folderPath . '/' . $file);
			}
		}
		return true;
	}
}

?>